package feeinterface.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ulwx.database.DbException;
import com.ulwx.tool.DbUtils;
import feeinterface.modeldb.BaseDynamicQuestionAnswer;

/**
 * 动态验证问题答案表业务操作
 * 
 * @author 黄杰
 * 
 */
public class BaseDynamicQuestionAnswerDao {

	/**
	 * 根据下行内容取得服务器动态验证随即一条答案
	 * 
	 * @param ProvinceID
	 *            省份ID
	 * @param question
	 *            问题字符串
	 * @return 答案
	 * @throws Exception
	 */
	public static BaseDynamicQuestionAnswer getAnswerByQuestion(String question)
			throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT * FROM BaseDynamicQuestionAnswer  ");
		sb.append("WHERE (Question LIKE CONCAT('%',?,'%')) and Question is not null"
				+ " and  Question!=''   AND (Answer is not null and "
				+ " Answer!='')  LIMIT 1 ");
		Map<Integer, Object> args = new HashMap<Integer, Object>();
		args.put(1, question);
		List<BaseDynamicQuestionAnswer> list = DbUtils.doQueryClass(null,
				BaseDynamicQuestionAnswer.class, sb.toString(), args);
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			sb = new StringBuffer();
			sb.append("SELECT * FROM BaseDynamicQuestionAnswer  ");
			sb.append("WHERE (? LIKE CONCAT('%',Question,'%')) and Question is not null"
					+ " and  Question!='' AND (Answer is not null and "
					+ " Answer!='')  LIMIT 1 ");
			list = DbUtils.doQueryClass(null, BaseDynamicQuestionAnswer.class,
					sb.toString(), args);
			if (list != null && list.size() > 0) {
				return list.get(0);

			}
		}
		return null;
	}
	
	public static void main(String[] args) throws Exception{
		getAnswerByQuestion("：中国的首都是哪里");
	}

}
